package com.onekbase.demo.tcp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class TcpGatewayUtils {

    public static String request(String body,String host, int port){
        try (Socket socket = new Socket(host, port);
             BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             PrintWriter output = new PrintWriter(socket.getOutputStream(), true);){
            output.println("gateway:"+body);
            String response = input.readLine();
            System.out.println("Server response: " + response);
            return response;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
